package com.parsechina.hpush.broker.internal.listener;

import com.parsechina.hpush.broker.internal.message.NotifyConnectMessage;
import com.parsechina.hpush.broker.parser.ConnectMessage;
import com.parsechina.hpush.broker.spi.ProtocolProcessor;

/**
 * @author linfeng
 * @date 2019-08-20
 **/
public class NotifyConnectMessageListener extends BaseMessageListener<NotifyConnectMessage> {


    NotifyConnectMessageListener(ProtocolProcessor protocolProcessor, String instanceId) {
        super(protocolProcessor, instanceId);
    }

    @Override
    public void onMessage(CharSequence channel, NotifyConnectMessage message) {

        if (!getInstanceId().equals(message.getUuid())
                && getProtocolProcessor().isExistClientOfServer(message.getClientId())) {

            ConnectMessage connectMessage = new ConnectMessage();
            connectMessage.setClientId(message.getClientId());
            connectMessage.setCleanSession(message.isCleanSession());

            getProtocolProcessor().forceCloseConnection(connectMessage);
            
        }

    }


}
